<?
require("required/dbconnect.php");
require("required/function.php");

require("required/header.php");
?>
<script src="required/js/jquery-1.4.2.js"></script>
<script src="required/js/common_javascript.js"></script>
<?
checkAccess("allow_product_management", 1);
if (!empty($_GET['updated'])) {
    $updated = $_GET['updated'];

    switch ($updated) {
        case 1:
            $msg = "<div class='dw_success'>Record Updated</div>";
            break;
        case 3:
            $msg = "<div class='dw_success'>Stock Updated</div>";
            break;
        case 4:
            $msg = "<div class='dw_success'>Image Deleted</div>";
            break;
        case 5:
            $msg = "<div class='dw_success'>Add-on Added</div>";
            break;
        case 6:
            $msg = "<div class='dw_failed'>Add-on Existed</div>";
            break;
        case 7:
            $msg = "<div class='dw_failed'>Add-on can't be same with the product</div>";
            break;
        case 8:
            $msg = "<div class='dw_success'>Add-on removed</div>";
            break;
        default:
            $msg = "unknown";
            break;
    }
    echo "<script>displayMsg('divMsg')</script>";
} else {
    $msg = "";
}

$id = $_GET['id'];
$get_id = mysql_query("select * from os_products where id=$id");

$a = mysql_fetch_array($get_id, MYSQL_ASSOC);

switch ($a['product_type']) {
    case "product":
        checkAccess("allow_product_management", 1);
        $title = "View Product";
        break;
    case "gift";
        checkAccess("allow_gift_management", 1);
        $title = "View Gift";
        break;
    default:
        checkAccess("allow_product_management", 1);
        $title = "View Product";
        break;
}
?>
<table width="100%"  cellpadding="0" cellspacing="5" height="300">

    <td width="84%" align="left" valign="top">

        <table width="100%">
            <td class="title" colspan="2"><?= $title ?></td>
<?
if ($a['cat_id'] != "") {
    $get_cat = mysql_query("select * from os_category where id=" . $a['cat_id']) or die(mysql_error());
    if (mysql_num_rows($get_cat) == 0) {
        $maincat = "<font color='red'>no category found!</font>";
    } else {
        $maincat = mysql_result($get_cat, 0, "name_eng");
    }
} else {
    $maincat = "<font color='red'>no category found!</font>";
}
?>
            <tr>
                <td colspan="3"> <div id="divMsg"><?= $msg ?></div></td>
            </tr>
            <tr>
                <td colspan="3" class="sub_title_line" ><br />Classification </td>
            </tr>
            <tr>
                <Td width="22%">Category</Td>
                <td width="2%">: </td><td width="76%">
<?= $maincat ?>
                </td>
            </tr>	
<?
if ($a['sub_cat_id'] != "") {

    $get_subcat = mysql_query("select * from os_sub_category where id=" . $a['sub_cat_id']) or die(mysql_error());


    if (mysql_num_rows($get_subcat) == 0) {
        $subcat = "<font color='red'>no sub category found!</font>";
    } else {
        $subcat = mysql_result($get_subcat, 0, "name_eng");
    }
} else {
    $subcat = "<font color='red'>no sub category found!</font>";
}
?>
            <tr>
                <Td width="22%">Sub Category</Td>
                <td width="2%">: </td><td>
            <?= $subcat ?>
                </td>
            </tr>
            <?
            if ($a['status'] == 0) {
                $status = "No";
            } else {
                $status = "Yes";
            }
            ?>
            <tr>
                <td>Display at website</td>
                <td>: </td><td>
<?= $status ?>
                </td>
            </tr>
<?
if ($a['show_at_latest'] == 0) {
    $show_at_latest = "No";
} else {
    $show_at_latest = "Yes";
}
?>
            <tr>
                <td>Display at latest</td>
                <td>: </td><td>
            <?= $show_at_latest ?>
                </td>
            </tr>

<?
if ($a['featured'] == 0) {
    $featured = "No";
} else {
    $featured = "Yes";
}
?>

            <tr>
                <td>Is Featured</td>
                <td>: </td><td>
            <?= $featured ?>
                </td>
            </tr>

            <tr>
                <td colspan="3" class="sub_title_line" ><br />Product Details </td>
            </tr>
            <tr>
                <td>Product name</td><td>:</td><td> <?= $a['product_name_eng'] ?></td>
            </tr>
            <tr>
                <td>Display name</td><td>:</td><td> <?= $a['display_name'] ?></td>
            </tr>
            <tr>
                <td>Clean URL</td><td>:</td><td> <?= $a['clean_url'] ?></td>
            </tr>
            <tr>
                <td>Product weight</td><td>:</td><td> <?= $a['product_weight'] ?> KG</td>
            </tr>
            <tr>
                <td>Product code</td><td>:</td><td> <?= $a['product_code'] ?></td>
            </tr>
            <tr valign="top">
                <td>Order ID</td><td>:</td><td><?= htmlspecialchars($a['order_id']) ?>						
                </td>
            </tr>

            <tr valign="top">
                <td>Description</td><td>:</td>
                <td valign="top"  >
<?= $a['description_eng'] ?>
                </td>
            </tr>
            <tr>

            <Tr valign="top">
                <Td>Youtube Video</Td>
                <td>:</td>
                <td>
<?
if ($a['youtube_id'] != "") {
    ?>
                        <img src='http://img.youtube.com/vi/<?= trim($a['youtube_id']) ?>/1.jpg'>
    <?
} else {
    ?>
                        No video added
<? } ?>
                </td>

            </Tr>
<?
if ($module_promotion == 1) {
    ?>
                <tr>
                    <td colspan="3" class="sub_title_line" ><br />Promotion </td>
                </tr>
                <tr>
                    <td>Promotion</td> <td>:</td>
                    <td>
    <?= convertYesNo($a['promotion']) ?>
                    </td>
                </tr>
    <?
    if ($module_promotion == 1) {
        if ($a['promotion'] == 1) {
            ?>

                        <tr valign="top">
                            <td width="264">Promotion Start</td> <td>:</td>
                            <td width="1341">
                                <?= $a['promotion_start'] ?>

                            </td>
                        </tr>
                        <tr valign="top">
                            <td width="264">Promotion End</td> <td>:</td>
                            <td width="1341">
            <?= $a['promotion_end'] ?>

                            </td>
                        </tr>
                        <?
                    }
                }

                if ($module_special_member == 1) {
                    ?>
                    <tr valign="top">
                        <td width="264">Promotion Open to</td> <td>:</td>
                        <td width="1341">
                            <?= convertMemberType($a['promotion_target']) ?>
                        </td>
                    </tr>
                    <?
                }
            }
            ?>

            <?
            if ($module_redeemable == 1) {
                ?>
                <tr>
                    <td colspan="3" class="sub_title_line" ><br />Redeemable Product</td>
                </tr>
                <tr>
                    <td>Redeemable</td><td>:</td>
                    <td>
    <?
    if ($a['redeemable'] == 0) {
        echo "No";
    } else {
        echo "Yes";
    }
    ?>
                    </td>
                </tr>
                <tr>
                    <td>Do Not Show at Product Page</td><td>:</td>
                    <td>
                <?
                if ($a['hide_at_products'] == 0) {
                    echo "No";
                } else {
                    echo "Yes";
                }
                ?>
                    </td>
                </tr>
                <tr>
                    <td>Points Required</td><td>:</td><td><?= $a['points_required'] ?></td>
                </tr>
            <? } ?>
            <tr>
                <td colspan="3">
                    <br><br>
            <?
            if ($a['product_type'] == "product") {
                if (checkAccess("allow_product_edit", 0)) {
                    ?>
                            <input type="button" value="Edit" name="btnsubmit" onClick="window.open('product_edit.php?id=<?= $id ?>','_self')" class="button"> 

        <?
    }

    if (checkAccess("allow_product_delete", 0)) {
        ?>
                            <input type="button" value="Delete" onClick="window.open('product_delete.php?id=<?= $id ?>','_self')" class="button"> 
                            <?
                        }
                    } else {
                        if (checkAccess("allow_gift_management", 0)) {
                            ?>
                            <input type="button" value="Edit" name="btnsubmit" onClick="window.open('product_edit.php?id=<?= $id ?>','_self')" class="button"> 

                            <input type="button" value="Delete" onClick="window.open('product_delete.php?id=<?= $id ?>','_self')" class="button"> 
        <?
    }
}
?>
                    <input type="button" value="Back" onClick="window.open('product_viewall.php?product_type=<?= $a['product_type'] ?>','_self')" class="button">
                </td>
            </tr>
            <tr>
                <td colspan="3" class="sub_title_line" ><br />Stock </td>
            </tr>

            
                    <?
                    $get_stock = mysql_query("select * from os_product_inventory where pid=" . $a['id']);
                    ?>
            <tr valign="top">
                <td colspan="3"> 
                    <table cellpadding="2" cellspacing="1" class="solidborder" width="500">
                        <tr class="table_header">
                            <td width="25">No</td>
                            <td width="91">Descripttion</td>
                           
                            <Td width="92">Unit Price</Td>
                           
                        </tr>
                    <?
                    if (mysql_num_rows($get_stock) == 0) {
                        ?>
                            <tr>
                                <td colspan="6" align="center"> - no stock added -</td>
                            </tr>
                        <?
                    } else {
                        $no = 1;
                        while ($s = mysql_fetch_array($get_stock, MYSQL_ASSOC)) {

                            if ($no % 2 == 0) {
                                $bgcolor = "#e5e5e5";
                            } else {
                                $bgcolor = "#eeeeee";
                            }
                            ?>	

                                <tr height="20" bgcolor="<?= $bgcolor ?>" onmouseover="this.style.backgroundColor='ccccff'" onmouseout="this.style.backgroundColor='<?= $bgcolor ?>'">	
                                    <Td><?= $no ?></Td>
                                    <td><?= $s['description'] ?></td>
                                    
                                    <td><?= $s['unit_price'] ?></td>
                                  				
                                </tr>

        <?
        $no++;
    }
}
?>
                    </table>
                    <br>
                    <input type="button" value="Update Stock" onClick="window.open('product_updatestock.php?id=<?= $id ?>','_self')" class="button"> 
                </td>
            </tr>


            <tr>
                <td colspan="3" class="sub_title_line" ><br />Product Add-On </td>
            </tr>


            <tr valign="top">
                <td colspan="3"> 
                    <div id="divAddOn">
                        <?
                        $get_add_on = mysql_query("select * from os_product_add_on where product_id='" . $a['id'] . "'");
                        ?>
                        <table cellpadding="2" cellspacing="1" class="solidborder" width="90%">
                            <tr class="table_header">
                                <td width="25">No</td>
                                <td width="91">Product Name</td>
                                <Td width="64">Product Id</Td>
                                <td width="50">Category</td>
                                <Td width="92">Price</Td>
                                <Td>Description</Td> 
                                <Td>Action</Td> 
                            </tr>
                        <?
                        if (mysql_num_rows($get_add_on) == 0) {
                            ?>
                                <tr>
                                    <td colspan="7" align="center"> - no add-on added -</td>
                                </tr>
                            <?
                        } else {
                            $no = 1;
                            while ($ao = mysql_fetch_array($get_add_on, MYSQL_ASSOC)) {
                                $get_product = mysql_query("select * from os_products where id='" . $ao['add_on_id'] . "'") or die(mysql_error());

                                $get_available_stock = mysql_query("select * from os_product_inventory where pid=" . $ao['add_on_id'] . " order by quantity desc limit 1") or die(mysql_error);

                                $aop = mysql_fetch_array($get_product, MYSQL_ASSOC);



                                if ($no % 2 == 0) {
                                    $bgcolor = "#e5e5e5";
                                } else {
                                    $bgcolor = "#eeeeee";
                                }

                                if (mysql_num_rows($get_available_stock) > 0) {
                                    $unit_price = number_format(mysql_result($get_available_stock, 0, "unit_price"), 2);
                                } else {
                                    $unit_price = "-";
                                }

                                $get_pic = mysql_query("select * from os_productpic where product_id=" . $ao['add_on_id'] . " order by order_id asc limit 1");

                                if (mysql_num_rows($get_pic) == 0) {
                                    $pic = "empty";
                                    $piclink = "noimage.jpg";
                                    $fix = "";
                                } else {
                                    $pic = mysql_fetch_array($get_pic, MYSQL_ASSOC);

                                    $piclink = $pic['name'];

                                    if ($pic["width"] - $pic["height"] > 0) {
                                        $fix = "width=100";
                                    } else {
                                        $fix = "height=100";
                                    }
                                }



                                $image_link = "../productpic/thumbnails/" . $piclink;
                                ?>	

                                    <tr height="20" bgcolor="<?= $bgcolor ?>" onmouseover="this.style.backgroundColor='ccccff'" onmouseout="this.style.backgroundColor='<?= $bgcolor ?>'" valign="top">	
                                        <Td><?= $no ?></Td>
                                        <td align="center">
                                            <img src='<?= $image_link ?>' <?= $fix ?>/>
                                            <br />
                                            <a href="product_viewfull.php?id=<?= $aop['id'] ?>"><?= $aop['product_name_eng'] ?></a></td>
                                        <td><?= $aop['product_code'] ?></td>
                                        <td><?= getDataById("os_category", "name_eng", "id", $aop['cat_id']) ?> -> <?= getDataById("os_sub_category", "name_eng", "id", $aop['sub_cat_id']) ?></td>
                                        <td>RM <?= $unit_price ?></td>
                                        <td><?= truncate($aop['description_eng'], 100) ?></td>	
                                        <td align="center"><a href="javascript:removeAddon(<?= $ao['id'] ?>)"><img src="images/icons/delete.gif" /></a></td>							
                                    </tr>


                                    <?
                                    $no++;
                                }
                            }
                            ?>
                        </table>
                        <br>

                    </div>

                    <form action="product_add_on.php" method="post">
                        <tr>
                            <Td colspan="2" align="left">
                                <font class="form_caption">Enter Product name and select a product from the list</font>

                            </td>

                        </tr>

                        <tr valign="top">

                            <td width="30%">
                                <link href="required/style_auto_complete.css" rel="stylesheet" type="text/css">
                                <script language="javascript">
                                    function viewvenue()
                                    {
                                        var venue_id = document.getElementById('venue_id').value;
                                        if(venue_id!="")
                                        {
                                            window.location="venu_view_full.php?id="+venue_id;	
                                        }else{
                                            alert("Please select from search result");
                                        }
                                    }
										
                                    function lookup(inputString) {
                                        if(inputString.length == 0) {
                                            // Hide the suggestion box.
                                            $('#suggestions').hide();
                                        } else {
                                            $.post("auto_complete_products.php", {queryString: ""+inputString+""}, function(data){
                                                if(data.length >0) {
                                                    $('#suggestions').show();
                                                    $('#autoSuggestionsList').html(data);
                                                }
                                            });
                                        }
                                    } // lookup
										
                                    function product_fill(product_id,product_name) {
                                        $('#product_search').val(product_name);
                                        $('#add_on_id').val(product_id);
                                        setTimeout("$('#suggestions').hide();", 200);
                                    }
										
										
                                    function removeAddon(addon_id)
                                    {
                                        if(confirm("Confirm to remove?"))
                                        {
                                            window.location="product_remove_add_on.php?id=<?= $id ?>&addon_id="+addon_id;	
                                        }
                                    }
                                </script>
                                <div>
                                    <input type="text" size="40" value="" id="product_search" onkeyup="lookup(this.value);" onblur="product_fill();" />
                                    <input type="hidden" id="add_on_id"  name="add_on_id" />
                                    <input type="hidden" id="product_id" name="product_id" value="<?= $id ?>" />

                                </div>
                                <div style="position:relative">
                                    <div class="suggestionsBox" id="suggestions" style="display: none;position:absolute">
                                        <img src="images/icons/upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
                                        <div class="suggestionList" id="autoSuggestionsList">
                                            &nbsp;
                                        </div>
                                    </div>
                                </div>
                            </td>
                        <tr>
                            <td>
                                <select name="action">
                                    <option value="1">Add selected product as add-on to current product</option>
                                    <option value="2">Add current product as add-on to selected product</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>

                                <input type="submit" value="Submit" />
                            </td>
                        </tr>
                    </form>
                </td>
            </tr>
            <tr>
                <td colspan="3" class="sub_title_line" ><br />Product Images </td>
            </tr>
            <tr>



<?
$get_photo = mysql_query("select * from os_productpic where product_id=$id order by order_id asc");
if (mysql_num_rows($get_photo) == 0) {
    ?>
                    <td valign="top" class="title" colspan="3">

                        <table width="100%" >
                            <tr>
                                <td width="100%" align="center" valign="middle" colspan="6">
                                    <br><br><br> - No Photo - <br> <br>
    <?
    if (checkAccess("allow_product_edit", 0)) {
        ?>
                                        <input type="button" value="Upload More" onclick="window.open('productpic_upload.php?action=new&aid=<?= $id ?>','_self')">
                                        <br><br><br> <br><br><br> 
    <? } ?>											

    <?
} else {
    ?>
                                <td valign="top" colspan="3">	
    <?
    if (checkAccess("allow_product_edit", 0)) {
        ?>
                                        <input type="button" value="Upload More" onclick="window.open('productpic_upload.php?action=new&aid=<?= $id ?>','_self')">
        <?
    }
    ?>										
                                    <table width="100%" cellpadding="10"  >

                                        <tr>
    <?
    $row = 0;
    $secondrow1 = "";
    $secondrow2 = "";

    while ($photo = mysql_fetch_array($get_photo, MYSQL_ASSOC)) {
        $piclink = $photo["name"];
        if ($photo["width"] - $photo["height"] > 0) {
            $fix = "width=100";
        } else {
            $fix = "height=100";
        }

        if ($row % 5 == 0) {
            $secondrow1 = "<tr>";
            $secondrow2 = "</tr>";
        } else {
            $secondrow1 = "";
            $secondrow2 = "";
        }
        $photo_id = $photo['id'];
        ?>
        <?= $secondrow1 ?>
                                                <td valign="top"  height="50%" align="center"><br>
                                                    <img src='../productpic/thumbnails/<?= $piclink ?>' <?= $fix ?> border="0" alt="<?= $photo['caption'] ?>">
                                                    <br>
                                                    Caption:<?= $photo['caption'] ?>
                                                    <br>
                                                    Order ID:<?= $photo['order_id'] ?>
                                                    <br>
                                                    Created:<?= $photo['create_datetime'] ?>				
                                                    <br>
        <?
        if (checkAccess("allow_product_edit", 0)) {
            ?>
                                                        <a href='productpic_upload.php?action=edit&aid=<?= $id ?>&pid=<?= $photo_id ?>'><img src="images/icons/edit.gif" border="0" /></a>
                                                        <a href='productpic_delete.php?aid=<?= $id ?>&pid=<?= $photo_id ?>'><img src="images/icons/delete.gif" border="0" /></a>
                        <? } ?>
                                                </td>

                        <?
                        $row++;
                    }
                    ?>
    <?= $secondrow2; ?>
    <?
}
?>
                                </table>
                            </td>
                    </table>


                </td>
            </tr>
        </table>
                                <? require("required/footer.php"); ?>